from django.db import models
from django.contrib.auth.models import AbstractUser


class Police(models.Model):
    """
    钉钉报警机器人
    """
    uid = models.AutoField(primary_key=True)
    # 钉钉返回token
    token = models.CharField(max_length=125, blank=True, null=True, unique=True)
    # 昵称
    nickname = models.CharField(max_length=64, blank=True, null=True, unique=True)
    # 备注
    remarks = models.CharField(max_length=256, blank=True, null=True)
    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    # 修改时间
    modify_time = models.DateTimeField(auto_now=True, blank=True, null=True)


class UserInfo(models.Model):
    """
    用户表
    """
    uid = models.AutoField(primary_key=True)
    username = models.CharField(max_length=24)
    password = models.CharField(max_length=24)
    level = models.CharField(max_length=24)
    # 昵称
    nickname = models.CharField(max_length=24, blank=True, null=True)
    # 备注
    remarks = models.CharField(max_length=256, blank=True, null=True)
    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    # 修改时间
    modify_time = models.DateTimeField(auto_now=True, blank=True, null=True)

    def __str__(self):
        return self.username


class TestInfo(models.Model):
    """
    手动创建的测试记录
    """
    uid = models.AutoField(primary_key=True)
    # 唯一标识
    request_id = models.CharField(max_length=64, blank=True, null=True)
    # 用例名
    case_name = models.CharField(max_length=64, blank=True, null=True)
    # 测试状态
    status = models.CharField(max_length=20, blank=True, null=True)
    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    # 修改时间
    modify_time = models.DateTimeField(auto_now=True, blank=True, null=True)
    # 测试工具
    tools = models.CharField(max_length=128, blank=True, null=True)
    # 清理环境
    clear = models.CharField(max_length=24, blank=True, null=True)
    # 备注信息
    remarks = models.CharField(max_length=256, blank=True, null=True)
    # 环境
    environment = models.CharField(max_length=64, blank=True, null=True)
    # 测试日志path
    log_path = models.CharField(max_length=255, blank=True, null=True)
    # 是否生成报告
    report_status = models.PositiveIntegerField(default=0, blank=True, null=True)
    # 测试报告path
    report_path = models.CharField(max_length=255, blank=True, null=True)
    # 记录点击stop之前的状态
    before_stop = models.CharField(max_length=20, blank=True, null=True)
    # 异常信息
    exception = models.CharField(max_length=128, blank=True, null=True)
    # 错误码
    error_code = models.CharField(max_length=24, blank=True, null=True)
    # 错误信息
    error_info = models.CharField(max_length=64, blank=True, null=True)
    # 执行用户
    execute_user = models.ForeignKey(
        to=UserInfo,
        to_field='uid',
        on_delete=models.CASCADE,
        db_column='execute_user'
    )
    # 报警token
    police = models.ForeignKey(
        to=Police, to_field='uid', db_column='police', blank=True, null=True, on_delete=models.SET_NULL
    )


class BaseScheduleInfo(models.Model):
    """
    定时任务
    """
    uid = models.AutoField(primary_key=True)
    # 唯一标识
    request_id = models.CharField(max_length=64, blank=True, null=True)
    # 用例名
    case_name = models.CharField(max_length=64, blank=True, null=True)
    # 测试状态
    status = models.CharField(max_length=20, blank=True, null=True)
    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    # 修改时间
    modify_time = models.DateTimeField(auto_now=True, blank=True, null=True)
    # 测试工具
    tools = models.CharField(max_length=128, blank=True, null=True)
    # 清理环境
    clear = models.CharField(max_length=24, blank=True, null=True)
    # 备注信息
    remarks = models.CharField(max_length=256, blank=True, null=True)
    # 环境
    environment = models.CharField(max_length=64, blank=True, null=True)
    # 测试日志path
    log_path = models.CharField(max_length=255, blank=True, null=True)
    # 是否生成报告
    report_status = models.PositiveIntegerField(default=0, blank=True, null=True)
    # 测试报告path
    report_path = models.CharField(max_length=255, blank=True, null=True)
    # 记录点击stop之前的状态
    before_stop = models.CharField(max_length=20, blank=True, null=True)
    # 确认数据一致性
    uniformity_status = models.CharField(max_length=8, default=0)
    # 异常信息
    exception = models.CharField(max_length=128, blank=True, null=True)
    # 错误码
    error_code = models.CharField(max_length=24, blank=True, null=True)
    # 错误信息
    error_info = models.CharField(max_length=64, blank=True, null=True)
    # 执行时间
    execute_time = models.DateTimeField(blank=True, null=True)
    # 执行用户
    execute_user = models.ForeignKey(
        to=UserInfo,
        to_field='uid',
        on_delete=models.CASCADE,
        db_column='execute_user'
    )
    # 报警token
    police = models.ForeignKey(
        to=Police, to_field='uid', db_column='police', blank=True, null=True, on_delete=models.SET_NULL
    )


class CaseManage(models.Model):
    """
    测试用例管理
    """
    uid = models.AutoField(primary_key=True)
    case_name = models.CharField(max_length=64, blank=True, null=True)
    # 测试用例文件名称, 不用加.py
    case_file_name = models.CharField(max_length=256, blank=True, null=True)
    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    # 修改时间
    modify_time = models.DateTimeField(auto_now=True, blank=True, null=True)
    # 备注
    remarks = models.CharField(max_length=256, blank=True, null=True)


class VersionManage(models.Model):
    """
    版本管理
    """
    uid = models.AutoField(primary_key=True)
    # 回归版本号
    version_name = models.CharField(max_length=64, blank=True, null=True, unique=True)
    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    # 修改时间
    modify_time = models.DateTimeField(auto_now=True, blank=True, null=True)
    # 备注
    remarks = models.CharField(max_length=256, blank=True, null=True)


class PackMange(models.Model):
    uid = models.AutoField(primary_key=True)
    # 包名
    pack_name = models.CharField(max_length=128, blank=True, null=True)
    # 包版本
    pack_version = models.CharField(max_length=64, blank=True, null=True)
    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    # 修改时间
    modify_time = models.DateTimeField(auto_now=True, blank=True, null=True)
    # 对应版本
    version = models.ForeignKey(
        to=VersionManage,
        to_field='uid',
        on_delete=models.CASCADE,
        db_column='version',
        related_name='pack'
    )


class Regression(models.Model):
    """
    回归测试主任务
    """
    uid = models.AutoField(primary_key=True)
    # 用例数
    case_number= models.PositiveIntegerField(default=0, blank=True, null=True)
    # 测试状态
    status = models.CharField(max_length=20, blank=True, null=True)
    # 测试成功计数
    success_number = models.PositiveIntegerField(default=0, blank=True, null=True)
    # 测试失败技术
    fail_number = models.PositiveIntegerField(default=0, blank=True, null=True)
    # 记录点击stop之前的状态
    before_stop = models.CharField(max_length=20, blank=True, null=True)
    # 备注信息
    remarks = models.CharField(max_length=256, blank=True, null=True)
    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    # 修改时间
    modify_time = models.DateTimeField(auto_now=True, blank=True, null=True)
    # 测试用例
    case_name_list = models.CharField(max_length=256, blank=True, null=True)
    # 版本号
    version_name = models.ForeignKey(
        to=VersionManage, to_field='uid', on_delete=models.CASCADE, db_column='version_name'
    )
    # 执行用户
    execute_user = models.ForeignKey(to=UserInfo, to_field='uid', on_delete=models.CASCADE, db_column='execute_user')
    # 报警token
    police = models.ForeignKey(
        to=Police, to_field='uid', db_column='police', blank=True, null=True, on_delete=models.SET_NULL
    )


class RegressionTestInfo(models.Model):
    """
    测试用例-测试记录, 用例的config都是固定的
    """
    uid = models.AutoField(primary_key=True)
    # 唯一标识
    request_id = models.CharField(max_length=64, blank=True, null=True)
    # 用例名
    case_name = models.CharField(max_length=64, blank=True, null=True)
    # 测试状态
    status = models.CharField(max_length=20, blank=True, null=True)
    # 测试日志path
    log_path = models.CharField(max_length=255, blank=True, null=True)
    # 记录点击stop之前的状态
    before_stop = models.CharField(max_length=20, blank=True, null=True)
    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    # 修改时间
    modify_time = models.DateTimeField(auto_now=True, blank=True, null=True)
    # 异常信息
    exception = models.CharField(max_length=128, blank=True, null=True)
    # 错误码
    error_code = models.CharField(max_length=24, blank=True, null=True)
    # 错误信息
    error_info = models.CharField(max_length=64, blank=True, null=True)
    # 执行用户
    execute_user = models.ForeignKey(
        to=UserInfo,
        to_field='uid',
        on_delete=models.CASCADE,
        db_column='execute_user'
    )
    regression = models.ForeignKey(
        to=Regression,
        to_field='uid',
        on_delete=models.CASCADE,
        db_column='regression',
        related_name='case_test'
    )
